<!DOCTYPE html>





<html class="theme-next gemini use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="generator" content="Hexo 3.9.0">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.3.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.3.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.3.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">
  <link rel="stylesheet" href="/lib/pace/pace-theme-big-counter.min.css?v=1.0.2">
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.3.0',
    exturl: false,
    sidebar: {"position":"right","display":"post","offset":12,"onmobile":false},
    back2top: {"enable":true,"sidebar":true,"scrollpercent":true},
    save_scroll: false,
    copycode: {"enable":true,"show_result":false,"style":null},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="description" content="Notehadoop&amp;#x5B8C;&amp;#x5168;&amp;#x5206;&amp;#x5E03;&amp;#x5F0F;&amp;#x96C6;&amp;#x7FA4;&amp;#x642D;&amp;#x5EFA;&amp;#x521A;&amp;#x5199;&amp;#x5B8C;&amp;#x3002;&amp;#x8FD8;&amp;#x662F;&amp;#x5148;&amp;#x5199;&amp;#x4E00;&amp;#x4E0B;&amp;#x5E94;&amp;#x7528;&amp;#x3002;&amp;#x8FD9;&amp;#x91C">
<meta name="keywords" content="spark">
<meta property="og:type" content="article">
<meta property="og:title" content="spark-sql-保存结果集到mysql">
<meta property="og:url" content="http://ithuhui.com/2018/11/11/spark-sql-save-to-mysql/index.html">
<meta property="og:site_name" content="ithuhui">
<meta property="og:description" content="Notehadoop&amp;#x5B8C;&amp;#x5168;&amp;#x5206;&amp;#x5E03;&amp;#x5F0F;&amp;#x96C6;&amp;#x7FA4;&amp;#x642D;&amp;#x5EFA;&amp;#x521A;&amp;#x5199;&amp;#x5B8C;&amp;#x3002;&amp;#x8FD8;&amp;#x662F;&amp;#x5148;&amp;#x5199;&amp;#x4E00;&amp;#x4E0B;&amp;#x5E94;&amp;#x7528;&amp;#x3002;&amp;#x8FD9;&amp;#x91C">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://ithuhui.com/2018/11/11/spark-sql-save-to-mysql/running-result.png">
<meta property="og:updated_time" content="2019-08-20T06:17:06.366Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="spark-sql-保存结果集到mysql">
<meta name="twitter:description" content="Notehadoop&amp;#x5B8C;&amp;#x5168;&amp;#x5206;&amp;#x5E03;&amp;#x5F0F;&amp;#x96C6;&amp;#x7FA4;&amp;#x642D;&amp;#x5EFA;&amp;#x521A;&amp;#x5199;&amp;#x5B8C;&amp;#x3002;&amp;#x8FD8;&amp;#x662F;&amp;#x5148;&amp;#x5199;&amp;#x4E00;&amp;#x4E0B;&amp;#x5E94;&amp;#x7528;&amp;#x3002;&amp;#x8FD9;&amp;#x91C">
<meta name="twitter:image" content="http://ithuhui.com/2018/11/11/spark-sql-save-to-mysql/running-result.png">
  <link rel="canonical" href="http://ithuhui.com/2018/11/11/spark-sql-save-to-mysql/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>spark-sql-保存结果集到mysql | ithuhui</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b7300352bcf49d6bfa3a57b8a0e37cdb";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>






  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  <div class="container sidebar-position-right">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">ithuhui</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">记录一切</p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-about">
      
    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    
  <div class="popup search-popup">
  <div class="search-header">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <div class="search-input-wrapper">
      <input autocomplete="off" autocorrect="off" autocapitalize="none"
             placeholder="搜索..." spellcheck="false"
             type="text" id="search-input">
    </div>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>
  <div id="search-result"></div>
</div>


  </div>
</div>
    </header>

    

  <a href="https://github.com/ithuhui" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content page-post-detail">
            

  <div id="posts" class="posts-expand">
    

  <article class="post" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://ithuhui.com/2018/11/11/spark-sql-save-to-mysql/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="冷大锤`">
      <meta itemprop="description" content="发牌的是上帝，出牌的是自己">
      <meta itemprop="image" content="/images/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="ithuhui">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">spark-sql-保存结果集到mysql

          
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2018-11-11 23:31:37" itemprop="dateCreated datePublished" datetime="2018-11-11T23:31:37+08:00">2018-11-11</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-08-20 14:17:06" itemprop="dateModified" datetime="2019-08-20T14:17:06+08:00">2019-08-20</time>
              </span>
            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/bigdata/" itemprop="url" rel="index"><span itemprop="name">bigdata</span></a></span>

                
                
              
            </span>
          

          
            <span id="/2018/11/11/spark-sql-save-to-mysql/" class="post-meta-item leancloud_visitors" data-flag-title="spark-sql-保存结果集到mysql" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
        
      
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2018/11/11/spark-sql-save-to-mysql/#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/2018/11/11/spark-sql-save-to-mysql/" itemprop="commentCount"></span></a>
  </span>
  
  
          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
              
                <span class="post-meta-item-text">本文字数：</span>
              
              <span>9.1k</span>
            </span>
          
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
              
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              
              <span>8 分钟</span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="Note"><a href="#Note" class="headerlink" title="Note"></a>Note</h1><p>hadoop&#x5B8C;&#x5168;&#x5206;&#x5E03;&#x5F0F;&#x96C6;&#x7FA4;&#x642D;&#x5EFA;&#x521A;&#x5199;&#x5B8C;&#x3002;&#x8FD8;&#x662F;&#x5148;&#x5199;&#x4E00;&#x4E0B;&#x5E94;&#x7528;&#x3002;&#x8FD9;&#x91CC;&#x5199;&#x4E00;&#x4E0B;sparksql&#x600E;&#x4E48;&#x5E94;&#x7528;&#x8D77;&#x6765;&#x3002;</p><a id="more"></a>
<p>Spark SQL&#x662F;spark&#x5BB6;&#x65CF;&#x91CC;&#x9762;&#x6700;&#x5E38;&#x7528;&#x7684;&#x3002;&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x5F53;&#x4E2D;&#x4E5F;&#x6BD4;RDD&#x5E38;&#x7528;&#x3002;</p>
<p>&#x9898;&#x5916;&#x8BDD;&#x3002;&#x60F3;&#x4E86;&#x89E3;RDD&#x7684;java&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;&#x53EF;&#x4EE5;&#x770B;</p>
<p><a href="https://ithuhui.com/2018/11/10/spark-rdd-common-java-api/">spark-rdd-JavaAPI&#x624B;&#x518C;(&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;)</a></p>
<ol>
<li><p>&#x6211;&#x4EEC;&#x5148;&#x770B;&#x4E00;&#x4E0B;wiki&#xFF08;&#x6765;&#x5386;&#xFF09;&#xFF1A;</p>
<p>Spark SQL&#x5728;Spark&#x6838;&#x5FC3;&#x4E0A;&#x5E26;&#x51FA;&#x4E00;&#x79CD;&#x540D;&#x4E3A;SchemaRDD&#x7684;&#x6570;&#x636E;&#x62BD;&#x8C61;&#x5316;&#x6982;&#x5FF5;&#xFF0C;&#x63D0;&#x4F9B;&#x7ED3;&#x6784;&#x5316;&#x548C;&#x534A;&#x7ED3;&#x6784;&#x5316;&#x6570;&#x636E;&#x76F8;&#x5173;&#x7684;&#x652F;&#x6301;&#x3002;Spark SQL&#x63D0;&#x4F9B;&#x4E86;&#x9886;&#x57DF;&#x7279;&#x5B9A;&#x8BED;&#x8A00;&#xFF0C;&#x53EF;&#x4F7F;&#x7528;Scala&#x3001;Java&#x6216;Python&#x6765;&#x64CD;&#x7EB5;SchemaRDDs&#x3002;&#x5B83;&#x8FD8;&#x652F;&#x6301;&#x4F7F;&#x7528;&#x4F7F;&#x7528;&#x547D;&#x4EE4;&#x884C;&#x754C;&#x9762;&#x548C;ODBC&#xFF0F;JDBC&#x670D;&#x52A1;&#x5668;&#x64CD;&#x4F5C;SQL&#x8BED;&#x8A00;&#x3002;&#x5728;Spark 1.3&#x7248;&#x672C;&#xFF0C;SchemaRDD&#x88AB;&#x91CD;&#x547D;&#x540D;&#x4E3A;DataFrame&#x3002;</p>
</li>
<li><p>&#x91CD;&#x70B9;&#x6CE8;&#x610F;</p>
<p>&#x4EE5;&#x524D;&#x9879;&#x76EE;&#x7ECF;&#x5E38;&#x80FD;&#x770B;&#x5230;Hive&#x89E3;&#x6790;MapReduce&#x63D0;&#x4EA4;&#x5230;&#x96C6;&#x7FA4;&#x4E0A;&#x8FD0;&#x884C;&#x3002;&#x540E;&#x6765;&#x51FA;&#x73B0;&#x4E86;Spark&#xFF0C;&#x53C8;&#x6709;&#x4E86;Hive&#x89E3;&#x6790;&#x6210;SparkJob&#x63D0;&#x4EA4;&#x5230;&#x96C6;&#x7FA4;&#x4E0A;&#x8FD0;&#x884C;&#x3002;&#x8FD9;&#x91CC;&#x89E3;&#x91CA;&#x4E00;&#x4E0B;</p>
<p>SparkSQL&#x7684;&#x524D;&#x8EAB;&#x662F;Shark&#xFF0C;&#x4F46;&#x53C8;&#x56E0;&#x4E3A;Shark&#x5BF9;&#x4E8E;Hive&#x7684;&#x592A;&#x591A;&#x4F9D;&#x8D56;&#xFF0C;2014&#x5E74;spark&#x56E2;&#x961F;&#x505C;&#x6B62;&#x5BF9;Shark&#x7684;&#x5F00;&#x53D1;&#xFF0C;&#x5C06;&#x6240;&#x6709;&#x8D44;&#x6E90;&#x653E;SparkSQL&#x9879;&#x76EE;&#x4E0A;&#xFF0C;SparkSQL&#x4F5C;&#x4E3A;Spark&#x751F;&#x6001;&#x7684;&#x4E00;&#x5458;&#x9010;&#x6E10;&#x53D1;&#x5C55;&#xFF0C;<strong>&#x800C;&#x4E0D;&#x518D;&#x53D7;&#x9650;&#x4E8E;Hive&#xFF0C;&#x53EA;&#x662F;&#x517C;&#x5BB9;Hive</strong>&#xFF1B;Hive on Spark&#x662F;&#x7531;Cloudera&#x53D1;&#x8D77;&#xFF0C;&#x7531;Intel&#x3001;MapR&#x7B49;&#x516C;&#x53F8;&#x5171;&#x540C;&#x53C2;&#x4E0E;&#x7684;&#x5F00;&#x6E90;&#x9879;&#x76EE;&#xFF0C;2014&#x5E74;spark&#x56E2;&#x961F;&#x505C;&#x6B62;&#x5BF9;Shark&#x7684;&#x5F00;&#x53D1;&#xFF0C;&#x5C06;&#x6240;&#x6709;&#x8D44;&#x6E90;&#x653E;SparkSQL&#x9879;&#x76EE;&#x4E0A;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;Hive&#x5C06;&#x4E0D;&#x518D;&#x53D7;&#x9650;&#x4E8E;&#x4E00;&#x4E2A;&#x5F15;&#x64CE;&#xFF0C;&#x53EF;&#x4EE5;&#x91C7;&#x7528;Map-Reduce&#x3001;Tez&#x3001;Spark&#x7B49;&#x5F15;&#x64CE;</p>
</li>
<li><p>&#x5E94;&#x7528;&#x573A;&#x666F;</p>
<p>&#x53EF;&#x4EE5;&#x8BF4;&#x5927;&#x6570;&#x636E;&#x5E94;&#x7528;&#x6765;&#x8BF4;&#x662F;&#x6700;&#x7B80;&#x5355;&#x7684;&#xFF0C;&#x6700;&#x65B9;&#x4FBF;&#x5E94;&#x7528;&#x7684;&#x8BA1;&#x7B97;&#x65B9;&#x5F0F;&#x3002;&#x901A;&#x8FC7;&#x5BF9;&#x6570;&#x636E;&#x6E90;&#x7684;&#x8BFB;&#x53D6;&#x540E;&#xFF0C;&#x4F7F;&#x7528;sql&#x8BED;&#x8A00;&#x5373;&#x53EF;&#x5206;&#x6790;&#x5E76;&#x89E3;&#x51B3;&#x5927;&#x90E8;&#x5206;&#x7684;&#x5927;&#x6570;&#x636E;&#x5206;&#x6790;&#x8BA1;&#x7B97;&#x95EE;&#x9898;&#x3002;&#x4E00;&#x822C;&#x5728;&#x6570;&#x636E;&#x5206;&#x5C42;&#x91CC;&#x9762;&#x4E0D;&#x7BA1;&#x662F;&#x6E90;&#x6570;&#x636E;&#x8BA1;&#x7B97;&#x8FD8;&#x662F;&#x4E1A;&#x52A1;&#x6570;&#x636E;&#x5206;&#x6790;&#x90FD;&#x5341;&#x5206;&#x5E38;&#x7528;&#x3002;</p>
</li>
<li><p>&#x5C0F;&#x7F16;&#x63D0;&#x9192;</p>
<p>&#x6211;github&#x7684;&#x4F8B;&#x5B50;&#x4E0D;&#x9700;&#x8981;&#x4F60;&#x53BB;&#x5B89;&#x88C5;spark&#x73AF;&#x5883;&#x3002;&#x5355;&#x673A;&#x7248;&#x7684;&#x5728;&#x672C;&#x673A;local&#x65E2;&#x53EF;&#x7528;&#x3002;</p>
<p>&#x4EE5;&#x540E;&#x66F4;&#x65B0;&#x5B8C;&#x6240;&#x6709;&#x7684;&#x57FA;&#x7840;&#x7528;&#x6CD5;&#x3002;&#x7528;&#x4E00;&#x4E2A;&#x9879;&#x76EE;&#x628A;&#x5168;&#x90E8;&#x5E94;&#x7528;&#x8D77;&#x6765;&#x3002;&#x5C31;&#x4E0D;&#x80FD;&#x7528;&#x5355;&#x673A;&#x7248;&#x8DD1;&#x4E86;&#x3002;</p>
<p>&#x4F46;&#x662F;&#x5B66;&#x4E60;&#x8FC7;&#x7A0B;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x88AB;&#x8FD9;&#x4E9B;&#x675F;&#x7F1A;&#x3002;</p>
<p>&#x6700;&#x5E38;&#x7528;&#x7684;&#x662F;&#x8BA1;&#x7B97;&#x7B2C;&#x4E00;&#x7B2C;&#x4E8C;&#x5C42;&#x6570;&#x636E;&#xFF0C;&#x5B58;hbase&#xFF0C;&#x7136;&#x540E;&#x8BA1;&#x7B97;&#x7B2C;&#x4E09;&#x5C42;&#x6570;&#x636E;&#xFF08;&#x4E1A;&#x52A1;&#x76F8;&#x5173;&#xFF09;&#xFF0C;&#x5B58;&#x5230;&#x7ED3;&#x679C;&#x8868;&#xFF08;ES or Mysql..etc&#xFF09;&#x3002;</p>
</li>
</ol>
<h1 id="Github"><a href="#Github" class="headerlink" title="Github"></a>Github</h1><p><strong>&#x5730;&#x5740;&#xFF1A;<a href="https://github.com/ithuhui/hui-base-java" target="_blank" rel="noopener">https://github.com/ithuhui/hui-base-java</a><br>&#x6A21;&#x5757;&#xFF1A;&#x3010;hui-base-spark&#x3011;<br>&#x5206;&#x652F;&#xFF1A;master<br>&#x4F4D;&#x7F6E;&#xFF1A;com.hui.base.spark.sparksql.job.MetroAnalysisJob</strong></p>
<h1 id="Ready"><a href="#Ready" class="headerlink" title="Ready"></a>Ready</h1><ol>
<li>maven&#x4F9D;&#x8D56; &#x6211;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;spark2.1.1</li>
<li>sql&#x57FA;&#x7840;</li>
<li>mysql&#xFF08;&#x5176;&#x4ED6;database&#x4E5F;&#x53EF;&#x4EE5;&#xFF0C;&#x7528;&#x4E8E;&#x8BA1;&#x7B97;&#x7ED3;&#x679C;&#x5B58;&#x50A8;&#xFF09;</li>
</ol>
<h1 id="Code"><a href="#Code" class="headerlink" title="Code"></a>Code</h1><h2 id="1-Mysql&#x6570;&#x636E;&#x5E93;&#x5EFA;&#x7ED3;&#x679C;&#x8868;"><a href="#1-Mysql&#x6570;&#x636E;&#x5E93;&#x5EFA;&#x7ED3;&#x679C;&#x8868;" class="headerlink" title="1. Mysql&#x6570;&#x636E;&#x5E93;&#x5EFA;&#x7ED3;&#x679C;&#x8868;"></a>1. Mysql&#x6570;&#x636E;&#x5E93;&#x5EFA;&#x7ED3;&#x679C;&#x8868;</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE `hui_metro_test` (</span><br><span class="line">  `id` int(11) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  `from_station` varchar(255) DEFAULT NULL,</span><br><span class="line">  `to_station` varchar(255) DEFAULT NULL,</span><br><span class="line">  `count` int(11) DEFAULT NULL,</span><br><span class="line">  `distance` double DEFAULT NULL,</span><br><span class="line">  PRIMARY KEY (`id`)</span><br><span class="line">) ENGINE=InnoDB AUTO_INCREMENT=201743 DEFAULT CHARSET=utf8;</span><br></pre></td></tr></table></figure>

<h2 id="2-DB&#x914D;&#x7F6E;&#x6587;&#x4EF6;"><a href="#2-DB&#x914D;&#x7F6E;&#x6587;&#x4EF6;" class="headerlink" title="2. DB&#x914D;&#x7F6E;&#x6587;&#x4EF6;"></a>2. DB&#x914D;&#x7F6E;&#x6587;&#x4EF6;</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">db.url=jdbc:mysql://127.0.0.1:3306/hui?characterEncoding=UTF-8</span><br><span class="line">db.user=root</span><br><span class="line">db.password=123456</span><br><span class="line">db.driver=com.mysql.jdbc.Driver</span><br></pre></td></tr></table></figure>

<h2 id="3-&#x641E;&#x4E2A;&#x6587;&#x4EF6;"><a href="#3-&#x641E;&#x4E2A;&#x6587;&#x4EF6;" class="headerlink" title="3. &#x641E;&#x4E2A;&#x6587;&#x4EF6;"></a>3. &#x641E;&#x4E2A;&#x6587;&#x4EF6;</h2><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">{ &quot;from_station&quot;:&quot;&#x897F;&#x6717;&quot;,&quot;to_station&quot;:&quot;&#x5751;&#x53E3;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;distance&quot;:&quot;1.6&quot; },</span><br><span class="line">{ &quot;from_station&quot;:&quot;&#x897F;&#x6717;&quot;,&quot;to_station&quot;:&quot;&#x82B1;&#x5730;&#x6E7E;&quot;,&quot;count&quot;:&quot;2&quot;,&quot;distance&quot;:&quot;2.5&quot; },</span><br><span class="line">{ &quot;from_station&quot;:&quot;&#x897F;&#x6717;&quot;,&quot;to_station&quot;:&quot;&#x82B3;&#x6751;&quot;,&quot;count&quot;:&quot;3&quot;,&quot;distance&quot;:&quot;3.8&quot; },</span><br><span class="line">{ &quot;from_station&quot;:&quot;&#x897F;&#x6717;&quot;,&quot;to_station&quot;:&quot;&#x9EC4;&#x6C99;&quot;,&quot;count&quot;:&quot;4&quot;,&quot;distance&quot;:&quot;5.2&quot; },</span><br><span class="line">{ &quot;from_station&quot;:&quot;&#x897F;&#x6717;&quot;,&quot;to_station&quot;:&quot;&#x957F;&#x5BFF;&#x8DEF;&quot;,&quot;count&quot;:&quot;5&quot;,&quot;distance&quot;:&quot;6.1&quot; },</span><br><span class="line">{ &quot;from_station&quot;:&quot;&#x897F;&#x6717;&quot;,&quot;to_station&quot;:&quot;&#x9648;&#x5BB6;&#x7960;&quot;,&quot;count&quot;:&quot;6&quot;,&quot;distance&quot;:&quot;7.3&quot; },</span><br><span class="line">{ &quot;from_station&quot;:&quot;&#x897F;&#x6717;&quot;,&quot;to_station&quot;:&quot;&#x897F;&#x95E8;&#x53E3;&quot;,&quot;count&quot;:&quot;7&quot;,&quot;distance&quot;:&quot;8.3&quot; },</span><br><span class="line">{ &quot;from_station&quot;:&quot;&#x897F;&#x6717;&quot;,&quot;to_station&quot;:&quot;&#x516C;&#x56ED;&#x524D;&quot;,&quot;count&quot;:&quot;8&quot;,&quot;distance&quot;:&quot;9.1&quot; },</span><br><span class="line">{ &quot;from_station&quot;:&quot;&#x897F;&#x6717;&quot;,&quot;to_station&quot;:&quot;&#x519C;&#x8BB2;&#x6240;&quot;,&quot;count&quot;:&quot;9&quot;,&quot;distance&quot;:&quot;10.3&quot; },</span><br><span class="line">{ <span class="attr">&quot;from_station&quot;</span>:<span class="string">&quot;&#x897F;&#x6717;&quot;</span>,<span class="attr">&quot;to_station&quot;</span>:<span class="string">&quot;&#x70C8;&#x58EB;&#x9675;&#x56ED;&quot;</span>,<span class="attr">&quot;count&quot;</span>:<span class="string">&quot;10&quot;</span>,<span class="attr">&quot;distance&quot;</span>:<span class="string">&quot;11.3&quot;</span> }</span><br></pre></td></tr></table></figure>

<h2 id="4-&#x6570;&#x636E;&#x5206;&#x5C42;"><a href="#4-&#x6570;&#x636E;&#x5206;&#x5C42;" class="headerlink" title="4. &#x6570;&#x636E;&#x5206;&#x5C42;"></a>4. &#x6570;&#x636E;&#x5206;&#x5C42;</h2><p>&#x6211;&#x8FD9;&#x91CC;&#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x6570;&#x636E;&#x5206;&#x5C42;&#xFF0C;&#x6839;&#x636E;&#x5B9E;&#x9645;&#x9700;&#x8981;&#x3002;&#x6211;&#x662F;&#x76F4;&#x63A5;&#x628A;&#x5206;&#x6790;&#x7ED3;&#x679C;&#x6587;&#x4EF6;&#x5B58;&#x5165;mysql</p>
<h2 id="5-SparkJob&#x7236;&#x7C7B;"><a href="#5-SparkJob&#x7236;&#x7C7B;" class="headerlink" title="5. SparkJob&#x7236;&#x7C7B;"></a>5. SparkJob&#x7236;&#x7C7B;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * &lt;b&gt;&lt;code&gt;SparkJob&lt;/code&gt;&lt;/b&gt;</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * Description:</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * &lt;b&gt;Creation Time:&lt;/b&gt; 2018/11/11 17:39.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Hu Weihui</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SparkJob</span> </span>{</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The constant LOGGER.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@since</span> hui_project 1.0.0</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger LOGGER = LoggerFactory.getLogger(SparkJob.class);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The constant serialVersionUID.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@since</span> hui_project 1.0.0</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">771902776566370732L</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Instantiates a new Spark job.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="title">SparkJob</span><span class="params">()</span></span>{}</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Execute.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> sparkContext the spark context</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> args         the args</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@since</span> hui_project 1.0.0</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">execute</span><span class="params">(JavaSparkContext sparkContext, String[] args)</span> </span>{</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Execute.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> sparkContext the spark context</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@since</span> hui_project 1.0.0</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">execute</span><span class="params">(JavaSparkContext sparkContext)</span> </span>{</span><br><span class="line"></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>

<h2 id="6-MetroAnalysisJob&#xFF08;&#x5177;&#x4F53;&#x4E1A;&#x52A1;sparkjob&#xFF09;"><a href="#6-MetroAnalysisJob&#xFF08;&#x5177;&#x4F53;&#x4E1A;&#x52A1;sparkjob&#xFF09;" class="headerlink" title="6. MetroAnalysisJob&#xFF08;&#x5177;&#x4F53;&#x4E1A;&#x52A1;sparkjob&#xFF09;"></a>6. MetroAnalysisJob&#xFF08;&#x5177;&#x4F53;&#x4E1A;&#x52A1;sparkjob&#xFF09;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * &lt;b&gt;&lt;code&gt;MetroAnalysisJob&lt;/code&gt;&lt;/b&gt;</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * Description:</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * &lt;b&gt;Creation Time:&lt;/b&gt; 2018/11/11 17:32.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Hu Weihui</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MetroAnalysisJob</span> <span class="keyword">extends</span> <span class="title">SparkJob</span> </span>{</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Logger LOGGER = LoggerFactory.getLogger(MetroAnalysisJob.class);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String INPUT_FILE_PATH</span><br><span class="line">            = MetroAnalysisJob.class.getClassLoader().getResource(<span class="string">&quot;test.json&quot;</span>).toString();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String OUTPUT_FILE_PATH</span><br><span class="line">            = <span class="string">&quot;D:/test/test&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String SQL = <span class="string">&quot;select * from hui_metro_testjson&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line">        SparkConf sparkConf = <span class="keyword">new</span> SparkConf()</span><br><span class="line">                .setAppName(<span class="string">&quot;test&quot;</span>)</span><br><span class="line">                .setMaster(<span class="string">&quot;local[4]&quot;</span>);</span><br><span class="line"></span><br><span class="line">        JavaSparkContext sparkContext = <span class="keyword">new</span> JavaSparkContext(sparkConf);</span><br><span class="line"></span><br><span class="line">        MetroAnalysisJob metroAnalysisJob = <span class="keyword">new</span> MetroAnalysisJob();</span><br><span class="line"></span><br><span class="line">        metroAnalysisJob.execute(sparkContext, args);</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">execute</span><span class="params">(JavaSparkContext sparkContext, String[] args)</span> </span>{</span><br><span class="line">        <span class="keyword">super</span>.execute(sparkContext, args);</span><br><span class="line">        deal(sparkContext, INPUT_FILE_PATH, OUTPUT_FILE_PATH);</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * &#x6570;&#x636E;&#x903B;&#x8F91;&#x5904;&#x7406;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> sparkContext</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> inPutPath</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> outPutPath</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">deal</span><span class="params">(JavaSparkContext sparkContext, String inPutPath, String outPutPath)</span> </span>{</span><br><span class="line">        SparkJobUtil.checkFileExists(inPutPath);</span><br><span class="line"></span><br><span class="line">        SQLContext sqlContext = <span class="keyword">new</span> SQLContext(sparkContext);</span><br><span class="line"><span class="comment">//        sqlContext.setConf(&quot;spark.sql.parquet.binaryAsString&quot;,&quot;true&quot;);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//&#x521B;&#x5EFA;&#x5FEB;&#x7167;&#x4E34;&#x65F6;&#x8868;</span></span><br><span class="line">        Dataset&lt;Row&gt; dataset = sqlContext.read().json(inPutPath);</span><br><span class="line">        dataset.registerTempTable(<span class="string">&quot;hui_metro_testjson&quot;</span>);</span><br><span class="line">        dataset.show(<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">        Dataset&lt;Row&gt; resultFrame = sqlContext.sql(SQL);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (resultFrame.count() &gt; <span class="number">0</span>) {</span><br><span class="line">            resultFrame.repartition(<span class="number">3</span>).write()</span><br><span class="line">                    .mode(SaveMode.Append).json(outPutPath);</span><br><span class="line">        }</span><br><span class="line"></span><br><span class="line">        resultFrame.show(<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//&#x7ED3;&#x679C;&#x5199;&#x5165;&#x6570;&#x636E;&#x5E93;</span></span><br><span class="line">        MySQLJdbcConfig jdbcConfig = <span class="keyword">new</span> MySQLJdbcConfig();</span><br><span class="line">        jdbcConfig.init();</span><br><span class="line">        resultFrame.write().mode(<span class="string">&quot;append&quot;</span>)</span><br><span class="line">                .jdbc(jdbcConfig.getUrl(), <span class="string">&quot;hui_metro_test&quot;</span>, jdbcConfig.getConnectionProperties());</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>

<h2 id="7-SparkJob&#x5DE5;&#x5177;&#x7C7B;"><a href="#7-SparkJob&#x5DE5;&#x5177;&#x7C7B;" class="headerlink" title="7. SparkJob&#x5DE5;&#x5177;&#x7C7B;"></a>7. SparkJob&#x5DE5;&#x5177;&#x7C7B;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * &lt;b&gt;&lt;code&gt;SparkJobUtil&lt;/code&gt;&lt;/b&gt;</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * Description:</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * &lt;b&gt;Creation Time:&lt;/b&gt; 2018/11/11 17:48.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Hu Weihui</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SparkJobUtil</span> </span>{</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The constant LOGGER.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@since</span> hui_project 1.0.0</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger LOGGER = LoggerFactory.getLogger(SparkJobUtil.class);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Close quietly.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> fileSystem the file system</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@since</span> hui_project 1.0.0</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">closeQuietly</span><span class="params">(FileSystem fileSystem)</span> </span>{</span><br><span class="line">        <span class="keyword">if</span> (fileSystem != <span class="keyword">null</span>) {</span><br><span class="line">            <span class="keyword">try</span> {</span><br><span class="line">                fileSystem.close();</span><br><span class="line">            } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line">                LOGGER.error(<span class="string">&quot;Fail to close FileSystem:&quot;</span> + fileSystem, e);</span><br><span class="line">            }</span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Check file exists.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> path the path</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@since</span> hui_project 1.0.0</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">checkFileExists</span><span class="params">(String path)</span> </span>{</span><br><span class="line">        Configuration configuration = <span class="keyword">new</span> Configuration();</span><br><span class="line">        FileSystem fileSystem = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> {</span><br><span class="line">            fileSystem = FileSystem.get(configuration);</span><br><span class="line">            <span class="keyword">if</span> (!fileSystem.exists(<span class="keyword">new</span> Path(path))) {</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> FileNotFoundException(path);</span><br><span class="line">            }</span><br><span class="line">        } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        }<span class="keyword">finally</span> {</span><br><span class="line">            closeQuietly(fileSystem);</span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>

<h2 id="8-MySQLjdbcConfig"><a href="#8-MySQLjdbcConfig" class="headerlink" title="8. MySQLjdbcConfig"></a>8. MySQLjdbcConfig</h2><p>&#x4E3B;&#x8981;&#x662F;&#x8BFB;&#x53D6;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x7136;&#x540E;&#x4F20;&#x5165;DataFrame&#x53BB;&#x8FDE;&#x63A5;</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * &lt;b&gt;&lt;code&gt;MySQLJdbcConfig&lt;/code&gt;&lt;/b&gt;</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * Description:</span></span><br><span class="line"><span class="comment"> * &lt;p/&gt;</span></span><br><span class="line"><span class="comment"> * &lt;b&gt;Creation Time:&lt;/b&gt; 2018/11/11 17:32.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Hu Weihui</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MySQLJdbcConfig</span> </span>{</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger LOGGER = LoggerFactory.getLogger(MySQLJdbcConfig.class);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String table;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String url;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Properties connectionProperties;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span>{</span><br><span class="line">        Properties properties = <span class="keyword">new</span> Properties();</span><br><span class="line">        InputStream resourceAsStream = <span class="keyword">this</span>.getClass().getClassLoader().getResourceAsStream(<span class="string">&quot;jdbc.properties&quot;</span>);</span><br><span class="line">        <span class="keyword">try</span> {</span><br><span class="line">            properties.load(resourceAsStream);</span><br><span class="line">            setUrl(properties.getProperty(<span class="string">&quot;db.url&quot;</span>));</span><br><span class="line">            <span class="comment">//&#x8003;&#x8651;&#x591A;&#x6570;&#x636E;&#x6E90;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x53E6;&#x5916;&#x521B;&#x5EFA;properties&#x4F20;&#x5165;</span></span><br><span class="line">            Properties connectionProperties = <span class="keyword">new</span> Properties();</span><br><span class="line">            connectionProperties.setProperty(<span class="string">&quot;user&quot;</span>,properties.getProperty(<span class="string">&quot;db.user&quot;</span>));</span><br><span class="line">            connectionProperties.setProperty(<span class="string">&quot;password&quot;</span>,properties.getProperty(<span class="string">&quot;db.password&quot;</span>));</span><br><span class="line">            connectionProperties.setProperty(<span class="string">&quot;url&quot;</span>,properties.getProperty(<span class="string">&quot;db.url&quot;</span>));</span><br><span class="line">            setConnectionProperties(connectionProperties);</span><br><span class="line">        } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line">            LOGGER.info(<span class="string">&quot;&#x8BFB;&#x53D6;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5931;&#x8D25;&quot;</span>);</span><br><span class="line">        }</span><br><span class="line"></span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getTable</span><span class="params">()</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> table;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTable</span><span class="params">(String table)</span> </span>{</span><br><span class="line">        <span class="keyword">this</span>.table = table;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getUrl</span><span class="params">()</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> url;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUrl</span><span class="params">(String url)</span> </span>{</span><br><span class="line">        <span class="keyword">this</span>.url = url;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Properties <span class="title">getConnectionProperties</span><span class="params">()</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> connectionProperties;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setConnectionProperties</span><span class="params">(Properties connectionProperties)</span> </span>{</span><br><span class="line"></span><br><span class="line">        <span class="keyword">this</span>.connectionProperties = connectionProperties;</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>

<h2 id="9-Running-Result"><a href="#9-Running-Result" class="headerlink" title="9. Running Result"></a>9. Running Result</h2><p><img src="/2018/11/11/spark-sql-save-to-mysql/running-result.png" alt="running-result"></p>

    </div>

    
    
    
        
      
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>冷大锤`</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://ithuhui.com/2018/11/11/spark-sql-save-to-mysql/" title="spark-sql-保存结果集到mysql">http://ithuhui.com/2018/11/11/spark-sql-save-to-mysql/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！</li>
</ul>
</div>

      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/spark/" rel="tag"># spark</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2018/11/11/hadoop-cluster-build/" rel="next" title="Hadoop完全分布式集群搭建">
                  <i class="fa fa-chevron-left"></i> Hadoop完全分布式集群搭建
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2018/11/18/hadoop-hive-build/" rel="prev" title="Hadoop-Hive-环境搭建">
                  Hadoop-Hive-环境搭建 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    <div class="comments" id="comments"></div>
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">

          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/images/head.png"
      alt="冷大锤`">
  <p class="site-author-name" itemprop="name">冷大锤`</p>
  <div class="site-description motion-element" itemprop="description">发牌的是上帝，出牌的是自己</div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">34</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/ithuhui" title="GitHub &rarr; https://github.com/ithuhui" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:hu545548724@163.com" title="E-Mail &rarr; mailto:hu545548724@163.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
  </div>




        </div>
      </div>
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Note"><span class="nav-number">1.</span> <span class="nav-text">Note</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Github"><span class="nav-number">2.</span> <span class="nav-text">Github</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Ready"><span class="nav-number">3.</span> <span class="nav-text">Ready</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Code"><span class="nav-number">4.</span> <span class="nav-text">Code</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-Mysql数据库建结果表"><span class="nav-number">4.1.</span> <span class="nav-text">1. Mysql数据库建结果表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-DB配置文件"><span class="nav-number">4.2.</span> <span class="nav-text">2. DB配置文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-搞个文件"><span class="nav-number">4.3.</span> <span class="nav-text">3. 搞个文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-数据分层"><span class="nav-number">4.4.</span> <span class="nav-text">4. 数据分层</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-SparkJob父类"><span class="nav-number">4.5.</span> <span class="nav-text">5. SparkJob父类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-MetroAnalysisJob（具体业务sparkjob）"><span class="nav-number">4.6.</span> <span class="nav-text">6. MetroAnalysisJob（具体业务sparkjob）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-SparkJob工具类"><span class="nav-number">4.7.</span> <span class="nav-text">7. SparkJob工具类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-MySQLjdbcConfig"><span class="nav-number">4.8.</span> <span class="nav-text">8. MySQLjdbcConfig</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-Running-Result"><span class="nav-number">4.9.</span> <span class="nav-text">9. Running Result</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2016 – <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">冷大锤`</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    
    <span title="站点总字数">238k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    
    <span title="站点阅读时长">3:36</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.3.0</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item-icon">
      <i class="fa fa-user"></i>
    </span>
    <span class="site-uv" title="总访客量">
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  
    <span class="post-meta-divider">|</span>
  
    <span class="post-meta-item-icon">
      <i class="fa fa-eye"></i>
    </span>
    <span class="site-pv" title="总访问量">
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>








        
      </div>
    </footer>

    

  </div>

  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

<script src="/js/utils.js?v=7.3.0"></script><script src="/js/motion.js?v=7.3.0"></script>

<script src="/js/schemes/pisces.js?v=7.3.0"></script>



<script src="/js/next-boot.js?v=7.3.0"></script>




  





  
  <script>
  function leancloudSelector(url) {
    return document.getElementById(url).querySelector('.leancloud-visitors-count');
  }
  if (CONFIG.page.isPost) {
    function addCount(Counter) {
      var visitors = document.querySelector('.leancloud_visitors');
      var url = visitors.getAttribute('id').trim();
      var title = visitors.getAttribute('data-flag-title').trim();

      Counter('get', `/classes/Counter?where=${JSON.stringify({ url })}`)
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            var counter = results[0];
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .then(response => response.json())
              .then(() => {
                leancloudSelector(url).innerText = counter.time + 1;
              })
            
              .catch(error => {
                console.log('Failed to save visitor count', error);
              })
          } else {
              Counter('post', '/classes/Counter', { title: title, url: url, time: 1 })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.log('Failed to create', error);
                });
            
          }
        })
        .catch(error => {
          console.log('LeanCloud Counter Error', error);
        });
    }
  } else {
    function showTime(Counter) {
      var visitors = document.querySelectorAll('.leancloud_visitors');
      var entries = [...visitors].map(element => {
        return element.getAttribute('id').trim();
      });

      Counter('get', `/classes/Counter?where=${JSON.stringify({ url: { '$in': entries } })}`)
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length === 0) {
            document.querySelectorAll('.leancloud_visitors .leancloud-visitors-count').forEach(element => {
              element.innerText = 0;
            });
            return;
          }
          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.url;
            var time = item.time;
            leancloudSelector(url).innerText = time;
          }
          for (var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = leancloudSelector(url);
            if (element.innerText == '') {
              element.innerText = 0;
            }
          }
        })
        .catch(error => {
          console.log('LeanCloud Counter Error', error);
        });
    }
  }

  fetch('https://app-router.leancloud.cn/2/route?appId=u7lzh9sG1gDGC8h3Ag4jEhxD-gzGzoHsz')
    .then(response => response.json())
    .then(({ api_server }) => {
      var Counter = (method, url, data) => {
        return fetch(`https://${api_server}/1.1${url}`, {
          method: method,
          headers: {
            'X-LC-Id': 'u7lzh9sG1gDGC8h3Ag4jEhxD-gzGzoHsz',
            'X-LC-Key': 'jRXlISNU0c9YNOrnTr60TQls',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        const localhost = /http:\/\/(localhost|127.0.0.1|0.0.0.0)/;
        if (localhost.test(document.URL)) return;
        addCount(Counter);
      } else if (document.querySelectorAll('.post-title-link').length >= 1) {
        showTime(Counter);
      }
    });
  </script>










  <script src="/js/local-search.js?v=7.3.0"></script>














  

  

  


  
  <script src="/js/scrollspy.js?v=7.3.0"></script><script src="/js/post-details.js?v=7.3.0"></script>



<script>
NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail';
  guest = guest.split(',').filter(item => {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: false,
    notify: false,
    appId: 'TIuoGjr6GXVsHwA9SzloOpGQ-gzGzoHsz',
    appKey: 'y6Va0n1Hs7FlD9RgvPCF70R3',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false,
    lang: '' || 'zh-cn',
    path: location.pathname
  });
}, window.Valine);
</script>

</body>
</html>
